SET(CUR_ARCH "aarch64")
# SET(CUR_ARCH "armhf")
# SET(CUR_ARCH "x86_64")
SET(PREFIX /home/sunjie/work)


SET(CMAKE_C_FLAGS "-O3 -fPIC -ldl -lpthread -lrt -fexceptions -march=armv8-a -mcpu=cortex-a53 -Wreorder -Wunused-parameter -Wall -Wextra -Wunused-parameter -Wl,--copy-dt-needed-entries")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++14 -fpermissive -DWITH_OPENSSL -DWITH_DOM -DWITH_NONAMESPACES -Wl,-unresolved-symbols=ignore-in-shared-libs")


SET(EXTERNAL_LIB "-lrt -ldl -lpthread")
message("external flags" ${EXTERNAL_LIB})

SET(STREAM_DIR ${PROJECT_SOURCE_DIR})
SET(OSD_DIR ${PROJECT_SOURCE_DIR}/../osd)
SET(UTIL_DIR ${PROJECT_SOURCE_DIR}/../base_util)
SET(COMMON_DIR ${PROJECT_SOURCE_DIR}/../common)
aux_source_directory(${UTIL_DIR} util_src)


SET(X3_USB_SOURCE
${util_src}
${COMMON_DIR}/common_tool.cpp
${COMMON_DIR}/mediakit_tool.cpp
${COMMON_DIR}/gb28181_tool.cpp

${STREAM_DIR}/base_puller.cpp
${STREAM_DIR}/horizon/base_osd.cpp
${STREAM_DIR}/horizon/osd_text.cpp
${STREAM_DIR}/horizon/stream_pusher.cpp
${STREAM_DIR}/horizon/stream_puller_base.cpp
${STREAM_DIR}/horizon/stream_puller_usb.cpp

${STREAM_DIR}/horizon/main_usb.cpp
)

SET(X3_NET_SOURCE
${util_src}
${COMMON_DIR}/common_tool.cpp
${COMMON_DIR}/mediakit_tool.cpp
${COMMON_DIR}/gb28181_tool.cpp

${STREAM_DIR}/base_puller.cpp
${STREAM_DIR}/horizon/base_osd.cpp
${STREAM_DIR}/horizon/osd_text.cpp
${STREAM_DIR}/horizon/stream_pusher.cpp
${STREAM_DIR}/horizon/stream_puller_base.cpp
${STREAM_DIR}/horizon/stream_puller_net.cpp

${STREAM_DIR}/horizon/main_net.cpp
)


SET(STREAM_USB_NAME stream.x3.usb)
SET(STREAM_NET_NAME stream.x3.net)
add_executable(${STREAM_USB_NAME} ${X3_USB_SOURCE})
add_executable(${STREAM_NET_NAME} ${X3_NET_SOURCE})


include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/..)
include_directories(${SDK_DIR})


# ffmpeg
SET(FFMPEG_PATH ${PREFIX}/lib/ffmpeg_build.${CUR_ARCH})
set(FFMPEG_LIB
    ${FFMPEG_PATH}/lib/libavcodec.so.58
    ${FFMPEG_PATH}/lib/libavformat.so.58
    ${FFMPEG_PATH}/lib/libavutil.so.56
    ${FFMPEG_PATH}/lib/libswresample.so.3
    ${FFMPEG_PATH}/lib/libx264.so.164
)
include_directories(${FFMPEG_PATH}/include)
target_link_libraries(${STREAM_USB_NAME} ${FFMPEG_LIB})
target_link_libraries(${STREAM_NET_NAME} ${FFMPEG_LIB})

# openssl
SET(OPENSSL_PATH "${PREFIX}/lib/openssl-1.1.1q/install.${CUR_ARCH}")
set(OPENSSL_LIB
    ${OPENSSL_PATH}/lib/libcrypto.so.1.1
    ${OPENSSL_PATH}/lib/libssl.so.1.1
)
include_directories(${OPENSSL_PATH}/include)
target_link_libraries(${STREAM_USB_NAME} ${OPENSSL_LIB})
target_link_libraries(${STREAM_NET_NAME} ${OPENSSL_LIB})

# libiconv
SET(ICONV_PATH "${PREFIX}/lib/libiconv-1.15/install.${CUR_ARCH}")
set(ICONV_LIB
    ${ICONV_PATH}/lib/libiconv.so.2
    ${ICONV_PATH}/lib/libcharset.so.1
)
include_directories(${ICONV_PATH}/include)
target_link_libraries(${STREAM_USB_NAME} ${ICONV_LIB})
target_link_libraries(${STREAM_NET_NAME} ${ICONV_LIB})

# x3sdk
# SET(X3SDK_PATH "${PREFIX}/lib/X3_SDK_v1/appsdk")
SET(X3SDK_PATH "${PREFIX}/lib/X3_RDK_v1/appsdk")
set(X3SDK_LIB
    ${X3SDK_PATH}/lib/hbmedia/libhbmedia.so
    ${X3SDK_PATH}/lib/hbmedia/libmultimedia.so
    ${X3SDK_PATH}/lib/hbmedia/libvio.so
    ${X3SDK_PATH}/lib/hbmedia/libisp.so
    ${X3SDK_PATH}/lib/hbmedia/libisp_algo.so
    ${X3SDK_PATH}/lib/hbmedia/libiar.so
    ${X3SDK_PATH}/lib/libion.so
    ${X3SDK_PATH}/lib/libhbmem.so
    ${X3SDK_PATH}/lib/libtinyalsa.so

    # ${X3SDK_PATH}/lib/hbbpu/libcnn_intf.so
    ${X3SDK_PATH}/lib/hbbpu/libdnn.so
    # ${X3SDK_PATH}/lib/hbbpu/libeasy_dnn.so
    ${X3SDK_PATH}/lib/hbbpu/libhbrt_bernoulli_aarch64.so
)
include_directories(${X3SDK_PATH}/include)
include_directories(${X3SDK_PATH}/include/vio)
target_link_libraries(${STREAM_USB_NAME} ${X3SDK_LIB})
target_link_libraries(${STREAM_NET_NAME} ${X3SDK_LIB})

# freetype
SET(FREE_PATH ${PREFIX}/lib/freetype-2.13.2/install.${CUR_ARCH})
set(FREE_LIB ${FREE_PATH}/lib/libfreetype.so.6.20.1)
include_directories(${FREE_PATH}/include/freetype2)
target_link_libraries(${STREAM_USB_NAME} ${FREE_LIB})
target_link_libraries(${STREAM_NET_NAME} ${FREE_LIB})

#harfbuzz
SET(HARF_PATH ${PREFIX}/lib/harfbuzz-2.7.2/install.${CUR_ARCH}.ft)
set(HARF_LIB ${HARF_PATH}/lib/libharfbuzz.so.0.20702.0)
include_directories(${HARF_PATH}/include/harfbuzz)
target_link_libraries(${STREAM_USB_NAME} ${HARF_LIB})
target_link_libraries(${STREAM_NET_NAME} ${HARF_LIB})

# opencv3.4.5
SET(OPENCV_PATH ${PREFIX}/lib/opencv-3.4.5/install.${CUR_ARCH}.freetype)
set(OPENCV_LIB ${OPENCV_PATH}/lib/libopencv_world.so.3.4)
include_directories(${OPENCV_PATH}/include)
target_link_libraries(${STREAM_USB_NAME} ${OPENCV_LIB})
target_link_libraries(${STREAM_NET_NAME} ${OPENCV_LIB})


# boost
SET(BOOST_PATH "${PREFIX}/lib/boost_1_67_0")
set(BOOST_LIB
    ${BOOST_PATH}/stage/lib.${CUR_ARCH}/libboost_filesystem.a
    ${BOOST_PATH}/stage/lib.${CUR_ARCH}/libboost_system.a
    ${BOOST_PATH}/stage/lib.${CUR_ARCH}/libboost_date_time.a
    ${BOOST_PATH}/stage/lib.${CUR_ARCH}/libboost_regex.a
)
include_directories(${BOOST_PATH})
target_link_libraries(${STREAM_USB_NAME} ${BOOST_LIB})
target_link_libraries(${STREAM_NET_NAME} ${BOOST_LIB})

# json
SET(JSONCPP_PATH ${PREFIX}/lib/jsoncpp-1.9.5/install.${CUR_ARCH})
set(JSONCPP_LIB ${JSONCPP_PATH}/lib/libjsoncpp.a)
include_directories(${JSONCPP_PATH}/include)
target_link_libraries(${STREAM_USB_NAME} ${JSONCPP_LIB})
target_link_libraries(${STREAM_NET_NAME} ${JSONCPP_LIB})

# spdlog
SET(SPDLOG_PATH ${PREFIX}/lib/spdlog-1.9.2/install.${CUR_ARCH})
set(SPDLOG_LIB ${SPDLOG_PATH}/lib/libspdlog.a)
include_directories(${SPDLOG_PATH}/include)
target_link_libraries(${STREAM_USB_NAME} ${SPDLOG_LIB})
target_link_libraries(${STREAM_NET_NAME} ${SPDLOG_LIB})

# yaml
SET(YAML_PATH ${PREFIX}/lib/yaml-cpp-yaml-cpp-0.6.2/install.${CUR_ARCH})
set(YAML_LIB ${YAML_PATH}/lib/libyaml-cpp.a)
include_directories(${YAML_PATH}/include)
target_link_libraries(${STREAM_USB_NAME} ${YAML_LIB})
target_link_libraries(${STREAM_NET_NAME} ${YAML_LIB})

target_link_libraries(${STREAM_USB_NAME} ${CMAKE_CXX_FLAGS} ${EXTERNAL_LIB})
target_link_libraries(${STREAM_NET_NAME} ${CMAKE_CXX_FLAGS} ${EXTERNAL_LIB})

